package inet.app.vod.adapter;

import inet.app.vod.activity.R;
import inet.app.vod.listeners.SelectCategoryVideoListener;
import inet.app.vod.listeners.VideoSummaryListener;
import inet.app.vod.object.FilmCommonModel;
import inet.app.vod.object.MovieModel;
import inet.app.vod.view.VideoSummaryView;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inet.app.framework.device.DeviceInfoModel;
import com.inet.app.framework.device.DeviceUtil;

public class FilmCommonAdapter extends ArrayAdapter<FilmCommonModel> {

	List<FilmCommonModel> mFilmCommonModels;
	Activity mContext;
	LayoutInflater mInflater;

	VideoSummaryListener mVideoSummaryListener;
	SelectCategoryVideoListener mSelectTypeListener;

	DeviceInfoModel mDevice;
	LinearLayout.LayoutParams mParams;
	RelativeLayout.LayoutParams mPosterParams;

	public FilmCommonAdapter(Activity context, int textViewResourceId,
			List<FilmCommonModel> models, VideoSummaryListener summaryListener,
			SelectCategoryVideoListener typeListener) {

		super(context, R.layout.item_listview_film_common, models);
		this.mContext = context;
		this.mInflater = this.mContext.getLayoutInflater();
		this.mFilmCommonModels = models;
		this.mVideoSummaryListener = summaryListener;
		this.mSelectTypeListener = typeListener;
		this.mDevice = DeviceUtil.getDeviceInfo((Activity) context);
		this.mParams = new LinearLayout.LayoutParams(mDevice.getWidth() / 3,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		/* Set image with ratio width/height = 1.5 and 3 images each row */
		this.mPosterParams = new RelativeLayout.LayoutParams(
				mDevice.getWidth() / 3, mDevice.getWidth() / 2);
		this.mPosterParams.setMargins(3, 0, 3, 0);
	}

	@Override
	public int getCount() {
		return mFilmCommonModels.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		final FilmCommonModel model = mFilmCommonModels.get(position);

		if (convertView == null) {

			convertView = mInflater.inflate(R.layout.item_listview_film_common,
					null);
			holder = new ViewHolder();
			holder.mTypeText = (TextView) convertView
					.findViewById(R.id.type_text);
			holder.mViewallBtn = (ImageButton) convertView
					.findViewById(R.id.arrow_image);
			holder.mListView = (LinearLayout) convertView
					.findViewById(R.id.slide_layout);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// Reset data
		holder.mTypeText.setText("");
		holder.mListView.removeAllViews();

		String categoryName = model.getCategory().getName();
		holder.mTypeText.setText(categoryName);
		holder.mViewallBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mSelectTypeListener != null) {
					mSelectTypeListener.onSelect(model.getCategory());
				}
			}
		});

		List<MovieModel> movieModels = model.getMovieModels();
		for (MovieModel movie : movieModels) {
			VideoSummaryView summaryView = new VideoSummaryView(mContext);
			summaryView.generateUIfromModel(movie, mVideoSummaryListener);
			summaryView.setLayoutParams(mParams);
			summaryView.mBackGround.setLayoutParams(mPosterParams);
			holder.mListView.addView(summaryView);
		}
		return convertView;
	}

	static class ViewHolder {
		public TextView mTypeText;
		public ImageButton mViewallBtn;
		public LinearLayout mListView;
	}

}
